package com.dz.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dz.reggie.common.BaseContext;
import com.dz.reggie.common.R;
import com.dz.reggie.entity.ShoppingCart;
import com.dz.reggie.service.ShoppingCartService;
import com.dz.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

// 购物车
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;


    /**
     * 购物车--添加菜品or套餐
     * @param shoppingCart 只保存有name，id，amount的信息
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart={}", shoppingCart.toString());
        // 查询用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 查询判断要修改的是菜品还是套餐
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        // 从数据库中获取要修改商品的详细信息 -- cartOne
        ShoppingCart cartOne = shoppingCartService.getOne(queryWrapper);
        if (cartOne != null){
            // 如果添加过则加1
            cartOne.setNumber(cartOne.getNumber() + 1);
            cartOne.setAmount(cartOne.getAmount().multiply(new BigDecimal(2)));
            shoppingCartService.updateById(cartOne);
        }else{
            // 未添加过则将商品加入购物车，并保存在数据库中
            shoppingCart.setNumber(1);
            cartOne = shoppingCart;
            shoppingCartService.save(cartOne);
        }
        return R.success(cartOne);
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> getList(){
        // 查询用户id
        log.info("请求购物车商品清单");
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(queryWrapper);
        return R.success(shoppingCartList);
    }

    // 清空购物车
    @DeleteMapping("/clean")
    public R<String> clear(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");

    }

    // 购物车及菜品中减少商品数量
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("shoppingCart：{}", shoppingCart);
        // 获取用户id
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        // 判断是菜品还是菜单
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);
        Long dishId = shoppingCart.getDishId();
        if (dishId != null){
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        }else{
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        // 查询出要修改的菜品或套餐详细信息
        ShoppingCart cartOne = shoppingCartService.getOne(queryWrapper);
        if (cartOne.getNumber() != 1){
            cartOne.setNumber(cartOne.getNumber() - 1);
            cartOne.setAmount(cartOne.getAmount().multiply(new BigDecimal(2)));
            shoppingCartService.updateById(cartOne);
        }else{
            // 数量为1，将该商品数量设为0，并在数据库中删除
            cartOne.setNumber(0);
            shoppingCartService.removeById(cartOne.getId());
        }
        return R.success(cartOne);

    }
}
